package at.jku.se.fastsell.db4o.transactions;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

import at.jku.se.fastsell.data.User;
import at.jku.se.fastsell.db4o.DatabaseException;

public class StoreUserTransaction extends Transaction<Void> {

	private User u;

	// --------------------------------------------------------------------------------------------

	public StoreUserTransaction(User u) {
		this.u = u;
	}

	// --------------------------------------------------------------------------------------------

	@Override
	public Void run() throws DatabaseException {
		// db.ext().store(u, 1);
		// commit();
		//

//		Key key = KeyFactory.createKey("User", "Fastsell");

		Entity userEntity = new Entity("User", key);
		userEntity.setProperty("name", u.getName());
		userEntity.setProperty("password", u.getPassword());
		
//		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		datastore.put(userEntity);
		return null;
	}

	// --------------------------------------------------------------------------------------------

	@Override
	public String toString() {
		return super.toString() + ": " + u;
	}

}
